/**
 * wdk.com Inc.
 * Copyright (c) 2004-2019 All Rights Reserved.
 */
package com.da.javatest.util;

import com.da.javatest.enums.ErrorCodeEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * 品仓商平台异常封装类
 */
public class SrpException extends RuntimeException {

    private static final long serialVersionUID = -610974778109004588L;
    /**
     * 异常错误代码
     */
    private ErrorCodeEnum errorCode = ErrorCodeEnum.UNKNOWN_EXCEPTION;
    /**
     * 错误信息
     */
    private String errorMessage;

    /**
     * 创建一个<code>SrpException</code>
     *
     * @param errorCode    错误码
     * @param errorMessage 错误描述
     */
    public SrpException(ErrorCodeEnum errorCode, String errorMessage) {
        super(errorMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    /**
     * 创建一个<code>SrpException</code>
     *
     * @param errorCode    错误码
     * @param errorMessage 错误描述
     * @param cause        异常
     */
    public SrpException(ErrorCodeEnum errorCode, String errorMessage, Throwable cause) {
        super(errorMessage, cause);
        this.errorCode = errorCode;
        this.errorMessage = StringUtils.isNotBlank(errorMessage) ? errorMessage
            : cause.getMessage();
    }

    /**
     * 创建一个<code>SrpException</code>
     *
     * @param errorCode 错误码
     */
    public SrpException(ErrorCodeEnum errorCode) {
        this(errorCode, errorCode.getDesc());
    }

    /**
     * 创建一个<code>SrpException</code>
     *
     * @param errorCode 错误码
     * @param cause     异常
     */
    public SrpException(ErrorCodeEnum errorCode, Throwable cause) {
        this(errorCode, null, cause);
    }

    /**
     * @see Throwable#toString()
     */
    @Override
    public final String toString() {
        String s = getClass().getName();
        String message = getLocalizedMessage();
        return s + ": " + errorCode.getErrorCode() + "[" + errorCode.getDesc() + "]" + ", message: "
            + message;
    }

    /**
     * Getter method for property <tt>errorCode</tt>.
     *
     * @return property value of errorCode
     */
    public ErrorCodeEnum getErrorCode() {
        return errorCode;
    }

    /**
     * Setter method for property <tt>errorCode</tt>.
     *
     * @param errorCode value to be assigned to property errorCode
     */
    public void setErrorCode(ErrorCodeEnum errorCode) {
        this.errorCode = errorCode;
    }

    /**
     * Getter method for property <tt>errorMessage</tt>.
     *
     * @return property value of errorMessage
     */
    public String getErrorMessage() {
        return errorMessage;
    }

    /**
     * Setter method for property <tt>errorMessage</tt>.
     *
     * @param errorMessage value to be assigned to property errorMessage
     */
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
}